import 'package:easy_refresh/easy_refresh.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/util/DbUtil.dart';

import '../../dao/ImSessionDao.dart';
import '../../pojo/Enums.dart';
import '../../util/ImConstant.dart';
import '../../util/ImSingleton.dart';

final class HotMessageTotalCubit extends Cubit<HotMessageTotalState> {
  HotMessageTotalCubit() : super(HotMessageTotalState.loading());

  Future<IndicatorResult> loadData() async {
    print("HotMessageCubit load!!");
    String userId = ImSingleton.getInstance().session.userId!;
    DateTime startDay = DateTime.now().add(Duration(days: -ImConstant.IM_SESSION_SHOW_DAYS));
    var sessionDao = ImSessionDao();
    int total = await sessionDao.sumHotMessageTotal(userId, startDay, 500);
    emit(HotMessageTotalState.success(total));
    return IndicatorResult.success;
  }

  Future<IndicatorResult> reload() async {
    print("HotMessageCubit reload!!");
    return await loadData();
  }

  void increaseHotMessageTotal(int total) {
    emit(HotMessageTotalState.success(this.state.hotMessageTotal + total));
  }
}

final class HotMessageTotalState extends Equatable {

  final LoadingState state;
  final int hotMessageTotal;

  const HotMessageTotalState._({
    this.state = LoadingState.loading,
    this.hotMessageTotal = 0,
  });

  const HotMessageTotalState.loading() : this._();

  const HotMessageTotalState.success(int total)
      : this._(state: LoadingState.success, hotMessageTotal: total);

  const HotMessageTotalState.failure() : this._(state: LoadingState.failure);

  @override
  List<Object?> get props => [state, hotMessageTotal];

}